// views/index/shop/certificate/paypage/third/index.js
var BASE_URL = getApp().globalData.BASE_URL
var util = require('../../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dingdan: '',
    id: '', //订单id
    huodong: "",
    zhuli_num: '', //还差几人助力
    huodongid: '', //活动详情页id
    ifzhuli: false,
    num: "", //已经助力人数
    zhuli_user: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.getSetting({
    //   withSubscriptions: true, //  这里设置为true,下面才mainSwitch
    //   success: function (res) {
    //     console.log(res)
    //     console.log(res.subscriptionsSetting)
    //     // 调起授权界面弹窗
    //     if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
    //       if (res.subscriptionsSetting.itemSettings != null) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
    //         let moIdState = res.subscriptionsSetting.itemSettings['GN_YE5unWBY4hsW96726WCHz4avKEVxPg4sGKIrNcCE','muS1weIb7AG0fT0QkEdnGVx9rJRN3v1vyO03SvY9Y1Q']; // 用户同意的消息模板id
    //         if (moIdState === 'accept') {
    //           console.log('接受了消息推送');

    //         } else if (moIdState === 'reject') {
    //           console.log("拒绝消息推送");

    //         } else if (moIdState === 'ban') {
    //           console.log("已被后台封禁");
    //         }
    //       } else {
    //         // 当用户没有点击 ’总是保持以上选择，不再询问‘  按钮。那每次执到这都会拉起授权弹窗
    //         wx.showModal({
    //           title: '提示',
    //           content: '请授权开通服务通知',
    //           showCancel: true,
    //           success: function (ress) {
    //             if (ress.confirm) {
    //               console.log(ress)
    //               wx.requestSubscribeMessage({ // 调起消息订阅界面
    //                 tmplIds: ['muS1weIb7AG0fT0QkEdnGVx9rJRN3v1vyO03SvY9Y1Q','GN_YE5unWBY4hsW96726WCHz4avKEVxPg4sGKIrNcCE'],
    //                 success(res) {
    //                   console.log('订阅消息 成功 ');
    //                   console.log(res);
    //                 },
    //                 fail(er) {
    //                   console.log("订阅消息 失败 ");
    //                   console.log(er);
    //                 }
    //               })
    //             }
    //           }
    //         })
    //       }
    //     } else {
    //       console.log('订阅消息未开启')
    //     }
    //   },
    //   fail: function (error) {
    //     console.log(error);
    //   },
    // })
    console.log(options, "助力页面的初始数据")
    var userinfo = wx.getStorageSync("USER")
    this.setData({
       id: options.id,
      huodongid: options.huodongid
    })
    var _this = this
    wx.request({
      url: `${BASE_URL}/pay/index`,
      method: "POST",
      data: {
        id: _this.data.id,
        type: 'xiaochengxv',
        uid: userinfo.id
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        if (res.data.zhuli_num) {
          _this.setData({
            zhuli_num: res.data.huodong.num - res.data.zhuli_num,
            ifzhuli: true
          })
          res.data.list_user.forEach(item => {
            item.avatar = `${BASE_URL}` + item.avatar
          })

        }

        if (res.data.list.is_paid==1) {
          console.log('助力好了',_this.data.id)
          wx.redirectTo({
            url: `./third/index?id=${_this.data.id}`
          })
        }
        res.data.list.time = util.formatTimeTwo(res.data.list.time, 'Y-M-D h:m:s');
        _this.setData({
          dingdan: res.data.list,
          huodong: res.data.huodong,
          num: res.data.zhuli_num,
        })
      }
    })
  },

 

  gopay: function () {
     var opId= wx.getStorageSync("openid")
    this.generateOrder(opId)
  },
  generateOrder: function (openid) {
    var that = this;
    wx.request({
      url: `${BASE_URL}/static/beecloud-php/demo/wx/wx.mini.php`,
      data: {
        type: 'pay',
        or_type: that.data.dingdan.type,
        openid: openid,
        amount: that.data.dingdan.amount,
        name: that.data.dingdan.name,
        id: that.data.id,
        title: that.data.dingdan.title
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (res) {
        console.log(res);
        if (res.data.resultCode != 0) {
          that.showInfo(res.data.errMsg);
          return;
        }
        that.pay(res.data.params);
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  pay: function (param) {
    var that = this;
    wx.requestPayment({
      timeStamp: param.timestamp,
      nonceStr: param.nonce_str,
      package: param.package,
      signType: param.sign_type,
      paySign: param.pay_sign,
      success: function (res) {
        wx.redirectTo({
          url: `./third/index?id=${that.data.id}`
        })
        // success
        console.log(res);
        that.showInfo('支付成功');
      },
      fail: function (res) {
        // fail
        console.log(res);
        var strMsg = res.errMsg;
        if (res.err_desc) {
          strMsg += ', ' + res.err_desc;
        }
        that.showInfo('您取消了支付');
      },
      complete: function () {
        // complete
        console.log("pay complete");
      }
    });
  },
  showInfo: function (msg) { //错误信息提示
    wx.showModal({
      title: '提示',
      showCancel: false,
      content: msg
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
  }

})